package com.epfresh.api.views.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;

import com.epfresh.supplier.R;

public class BaseDialogView {
    public interface OnDismissListener {
        public void onDismiss(Object o);
    }

    protected Context context;
    protected ViewGroup contentContainer;
    protected ViewGroup decorView;//activity的根View
    protected ViewGroup rootView;//附加View 的 根View

    protected OnDismissListener onDismissListener;
    protected boolean isDismissing;

    protected Animation outAnim1;
    protected Animation inAnim1;
    protected Animation outAnim;
    protected Animation inAnim;
    protected int gravity = Gravity.RIGHT;

    protected int rootViewBg = Color.parseColor("#60000000");

    public BaseDialogView(Context context) {
        this.context = context;
        initBefore();
        initViews();
        initAnim();
        initEvents();
    }

    public void initBefore(){

    }

    protected void initViews() {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        decorView = (ViewGroup) ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content);
        rootView = (ViewGroup) layoutInflater.inflate(R.layout.layout_base_dialog_view, decorView, false);
        rootView.setLayoutParams(new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
        ));
        rootView.setBackgroundColor(rootViewBg);
        contentContainer = (ViewGroup) rootView.findViewById(R.id.content_container);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, gravity
        );
        contentContainer.setLayoutParams(params);
    }

    protected void initAnim() {
        inAnim = getInAnimation();
        outAnim = getOutAnimation();
        inAnim1 = new AlphaAnimation(0.0f,1.0f);
        inAnim1.setDuration(300);
        outAnim1 = new AlphaAnimation(1.0f,0.0f);
        outAnim1.setDuration(300);
    }

    public void setGravity(int gravity) {
        this.gravity = gravity;
    }

    protected void initEvents() {
    }

    public void setRootViewBackground(int color){
        this.rootViewBg = color;
    }

    /**
     * show的时候调用
     *
     * @param view 这个View
     */
    private void onAttached(View view) {
        decorView.addView(view);
        contentContainer.startAnimation(inAnim);
        rootView.startAnimation(inAnim1);
    }



    /**
     * 添加这个View到Activity的根视图
     */
    public void show() {
        if (isShowing()) {
            return;
        }
        onAttached(rootView);
    }

    /**
     * 检测该View是不是已经添加到根视图
     *
     * @return 如果视图已经存在该View返回true
     */
    public boolean isShowing() {
        View view = decorView.findViewById(R.id.outmost_container);
        return view != null;
    }

    public void dismiss() {
        if (isDismissing) {
            return;
        }

        rootView.startAnimation(outAnim1);
        //消失动画
        outAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                decorView.post(new Runnable() {
                    @Override
                    public void run() {
                        //从activity根视图移除
                        decorView.removeView(rootView);
                        isDismissing = false;
                        if (onDismissListener != null) {
                            onDismissListener.onDismiss(BaseDialogView.this);
                        }
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        contentContainer.startAnimation(outAnim);
        isDismissing = true;
    }

    public Animation getInAnimation() {
        int res = getAnimationResource(this.gravity, true);
        return AnimationUtils.loadAnimation(context, res);
    }

    public Animation getOutAnimation() {
        int res = getAnimationResource(this.gravity, false);
        return AnimationUtils.loadAnimation(context, res);
    }

    public BaseDialogView setOnDismissListener(OnDismissListener onDismissListener) {
        this.onDismissListener = onDismissListener;
        return this;
    }

    public BaseDialogView setCancelable(boolean isCancelable) {
        View view = rootView.findViewById(R.id.outmost_container);

        if (isCancelable) {
            view.setOnTouchListener(onCancelableTouchListener);
        } else {
            view.setOnTouchListener(null);
        }
        return this;
    }

    /**
     * Called when the user touch on black overlay in order to dismiss the dialog
     */
    private final View.OnTouchListener onCancelableTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                dismiss();
            }
            return false;
        }
    };

    public View findViewById(int id) {
        return contentContainer.findViewById(id);
    }

    private int INVALID = -1;

    public int getAnimationResource(int gravity, boolean isInAnimation) {
        switch (gravity) {
            case Gravity.BOTTOM:
                return isInAnimation ? R.anim.push_up_in : R.anim.push_up_out;
            case Gravity.RIGHT:
                return isInAnimation ? R.anim.anim_right_push_in : R.anim.anim_right_push_out;

        }
        return INVALID;
    }
}
